import SimpleITK as sitk

now=0
outTx=sitk.BSplineTransform( sitk.ReadTransform('3reg'+str(now)+'.mat') )
# print(outTx)
fixed=sitk.ReadImage('Masked/'+str(now)+'masked.nii',sitk.sitkFloat32)
toDisplacementFilter=sitk.TransformToDisplacementFieldFilter()
toDisplacementFilter.SetReferenceImage(fixed)
displacementField=toDisplacementFilter.Execute(outTx)
# print(displacementField)

dTx=sitk.DisplacementFieldTransform(displacementField)
print(dTx)
exit(0)
moving=sitk.ReadImage('Masked/'+str(3)+'masked.nii',sitk.sitkFloat32)
resampler = sitk.ResampleImageFilter()
resampler.SetInterpolator(sitk.sitkLinear)
resampler.SetDefaultPixelValue(800)
resampler.SetTransform(outTx)
resampler.SetOutputPixelType(sitk.sitkInt16)

out = resampler.Execute(moving)
print(out)
sitk.WriteImage(out,'3TxUsingDTx.nii')

#same img but opposite order?